package com.km.team.phsystem.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.Collections.sort;

/**
 * @author ：bastr.
 * @description：获取每年国务院发布的假日信息
 * @date ：2018/10/19
 */
public class SystemDateUtils {
    private static final String URL = "http://tool.bitefu.net/jiari/?d=";

    /**
     * 获取当年假日json数据
     *
     * @return
     */
    public static String getHolidayStr() {
        int year = LocalDate.now().getYear();
        return HttpUtil.get(URL + year);
    }

    /**
     * 获取假日列表
     * @param holidayStr 假日JSON数据
     * @return
     */
    public static List<String> getHolidayList(String holidayStr) {
        Map yearMap = JSONObject.parseObject(holidayStr, HashMap.class);
        String year = new ArrayList<String>(yearMap.keySet()).get(0);
        Map<String, String> holdiayMap = JSONObject.parseObject(yearMap.values().iterator().next().toString(), HashMap.class);
        ArrayList<String> holidays = new ArrayList<>(holdiayMap.keySet());
        sort(holidays);
        return holidays.stream().map(value -> {
            String s1 = year + value;
            return DateUtil.format(DateUtil.parse(s1), "yyyy-MM-dd");
        }).collect(Collectors.toList());
    }
}
